
import { _decorator, Component, Node, dragonBones, Vec2, Vec3 } from 'cc';
const { ccclass, property } = _decorator;
const ANI_GROUP = "normal";

import { Common } from './common';
// 实例化
const COM = new Common();
@ccclass('Enemy')
export class Enemy extends Component {
	// [1]
	// dummy = '';

	// [2]
	// @property
	// serializableDummy = 0;
	// private _curPos: Vec2 = new Vec2();
	start() {

		// console.log(this.node)
		// [3]
		// 碰撞范围。x从中间算起，y从底部算起
		this.node.cW = { x: this.node.width / 2, y: this.node.height }

if(this.node.getChildByName("body")!=null){
		// 获取子节点的龙骨
		this.dbDisplay = this.node.getChildByName("body").getComponent(dragonBones.ArmatureDisplay);
		// 动画
		this.dbArmature = this.dbDisplay.armature();

		// 动画播放完毕
		this.dbDisplay.on(dragonBones.EventObject.COMPLETE, this.aniComplete, this);
		this.dbDisplay.on(dragonBones.EventObject.FRAME_EVENT, this.frameEvent, this);
	}
		this.Game = this.node.parent.getComponent("Game")

		this.Hero = this.node.parent.getComponent("Hero")

		this.hero = this.node.parent.getChildByName("hero")

		//把脸朝向主角
		//敌人的脸方向，跟主角是相反的
		this.node.isDie = false; //是否死了
		this.moveSpeed = 3;
		this.speed = 0;
		this.thinkTime = 2; //思考时间
		this.thinkCurTime = 0; //思考时间累加
		this.State = Common.State.IDLE;
		//新建血量之类的属性
		//今天就先做血量吧。



		this.init()

	}


	init() {
		this.ROLE = {
			hp: 100,
			att: 5
		}
	}


	//下一步动作
	nextStep() {
		if (this.hero==null||this.hero.isDie) return;
		this.checkDir();
		if (Common.checkCollide(this.hero, this.node)) {
			this.speed = 0;
			this.attackAni();
			return;
		}
		this.dbArmature.animation.fadeIn("move", -1, -1, 0, ANI_GROUP);
		this.State = Common.State.MOVE;
		this.speed = this.moveSpeed * -this.node.getScale().x;
	}


	//判断行走方向
	checkDir() {
		let scale = this.node.getPosition().x > this.hero.getPosition().x ? 1 : -1;

		let faceDir = new Vec2()
		this.node.getScale(faceDir)
		faceDir.x = scale;
		this.node.setScale(faceDir);

	}

	aniComplete(e) {
		this.idleAni();
	}
	idleAni() {
		if (this.node.isDie) return;
		this.State = Common.state.IDLE;
		this.dbArmature.animation.fadeIn("idle", -1, -1, 0, ANI_GROUP, dragonBones.AnimationFadeOutMode.All);
	}


	frameEvent(e) {
		if (e.name == "attack") {
			if (Common.checkCollide(this.hero, this.node)) {
				this.hero.getComponent("Hero").uatt(this.ROLE.att);
			}
		}
	}

	removeOn() {
		this.dbDisplay.off(dragonBones.EventObject.COMPLETE, this.aniComplete, this);
		this.dbDisplay.off(dragonBones.EventObject.FRAME_EVENT, this.frameEvent, this);
	}



	//受到攻击时
	uatt(att) {
		//执行game里面的showHit涵数
		//txt的初始坐示，是敌人的坐标一样，高度是敌人的y坐标加上高度
		if (this.node.isDie) return; //如果已经死了，就不往下执行了
		// this.node.stopAllActions();
		this.ROLE.hp -= att; //敌人的HP减去等于
		//如果敌人已经死亡
		if (this.ROLE.hp <= 0) {

			Common.playAudio(Common.audioSource.die_enemy,true,0.1);
			this.removeOn();
			this.node.isDie = true;
			this.dbDisplay.playAnimation("die"); //播放死亡动
			this.scheduleOnce(() => {
				this.node.destroy();
			}, 2);
			this.hero.ROLE.score+=10;
			this.Game.score.string = "分数：" + this.hero.ROLE.score;			
			Common.setScore(this.hero.ROLE.score)
			return;//不再往下执行了
		}
		
		// this.node.getPosition(pos);
		
		this.Game.showHit(att);
		if (this.State != Common.State.ATTACK) this.dbArmature.animation.fadeIn("hit", -1, -1, 0, ANI_GROUP);
	}

	idleAni() {
		if (this.node.isDie) return;
		this.State = Common.State.IDLE;
		this.dbArmature.animation.fadeIn("idle", -1, -1, 0, ANI_GROUP, dragonBones.AnimationFadeOutMode.All);
	}

	attackAni() {
		if (this.hero.isDie) return;
		this.State = Common.State.ATTACK;
		this.dbArmature.animation.fadeIn("att_1", -1, -1, 0, ANI_GROUP);
		Common.playAudio(Common.audioSource.hit_enemy,true,0.1)
	}

	move() {		
		if (Common.checkCollide(this.hero, this.node)) {
			this.idleAni();
			this.speed = 0;
			this.attackAni();
			return;
		}

		let Pos = new Vec3()
		this.node.getPosition(Pos);
		Pos.x += this.speed;
		this.node.setPosition(Pos);

		// this.node.x += this.speed;
	}


	update(deltaTime: number) {
		// console.log(333)
		// [4]
		if (this.node.isDie) return;
		if (this.State == Common.State.IDLE) {
			this.thinkCurTime += deltaTime;
			if (this.thinkCurTime >= this.thinkTime) {
				this.thinkCurTime = 0;
				this.nextStep();
			}
		}
		if (this.speed != 0) this.move();
	}
}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
